﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.ServiceHelper.Excel;
using Kingdee.BOS.App.Data;

namespace JPDPROJECT
{

    /// <summary>
    /// 上传单据体数据文件
    /// </summary>
    /// <remarks>
    /// 界面说明：
    /// 1. 添加一个文件上传控件，设置文件后缀，必须为*.xls
    /// 2. 添加确定、取消两个按钮
    /// 3. 默认情况下，确定按钮时锁定的，只有上传文件完毕，才解锁
    /// </remarks>
    [Description("上传单据体数据文件")]
    public class ImportFileUpdateEdit : AbstractDynamicFormPlugIn
    {
        /// <summary>
        /// 上传上来的文件名：完整的文件名，包含了物理路径
        /// </summary>
        private string _fullFileName = string.Empty;
        public override void AfterBindData(EventArgs e)
        {
            this.View.GetControl("F_JD_BTNOK").Enabled = false;
        }

        /// <summary>
        /// 文件上传完毕，触发此事件：通过此事件获取上传上来的文件名
        /// </summary>
        /// <param name="e"></param>
        public override void CustomEvents(CustomEventsArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_PAEZ_FileUpdate"))
            {
                this.View.GetControl("F_PAEZ_FileUpdate").SetCustomPropertyValue("NeedCallback", true);
                this.View.GetControl("F_PAEZ_FileUpdate").SetCustomPropertyValue("IsRequesting", false);

                if (e.EventName.EqualsIgnoreCase("FileChanged"))
                {
                    // 文件上传完毕
                    // 取文件上传参数，文件名
                    JSONObject uploadInfo = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);

                    if (uploadInfo != null)
                    {
                        JSONArray json = new JSONArray(uploadInfo["NewValue"].ToString());

                        if (json.Count > 0)
                        {
                            // 取上传的文件名
                            string fileName = (json[0] as Dictionary<string, object>)["ServerFileName"].ToString();

                            this._fullFileName = this.GetFullFileName(fileName);
                            // 解锁确定按钮
                            this.View.GetControl("F_JD_BTNOK").Enabled = true;
                        }
                        else
                        {
                            // 锁定确定按钮
                            this.View.GetControl("F_JD_BTNOK").Enabled = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="e"></param>
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_JD_BTNOK"))
            {

                int i;
                string F_PAEZ_FBiILLNO = "";
                string F_PAEZ_WLNO = "";
                string FYF = "";
                string strsql = "";
                string FCount;

                using (ExcelOperation helper = new ExcelOperation(this.View))
                // 利用ExcelOperation对象，把xml文件，转为DataSet对象
                // 参数说明：
                // filePath : 完整的文件名，包含了物理目录
                // dataStartIndex : 数据开始行索引，从0开始。通常第一行为标题，第二行开始为数据行
                // colNameIndex : 列名所在行索引，从0开始。如此参数为0，表明第一行为列名行
                {
                    DataSet ds = helper.ReadFromFile(this._fullFileName, 1, 0);

                    DataTable dt = ds.Tables[0];


                    for (i = this.Model.GetEntryRowCount("F_PAEZ_Entity"); i >= 0; i--)
                    {

                        this.Model.DeleteEntryRow("F_PAEZ_Entity", i);

                    }
                    int rowIndex = this.Model.GetEntryRowCount("F_PAEZ_Entity");
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        this.Model.CreateNewEntryRow("F_PAEZ_Entity");
                        this.Model.SetEntryCurrentRowIndex("F_PAEZ_Entity", rowIndex);

                        this.Model.SetValue("F_PAEZ_FBiILLNO", dataRow["拣配单号"], rowIndex);
                        this.Model.SetValue("F_PAEZ_WLNO", dataRow["物流单号"], rowIndex);


                        F_PAEZ_FBiILLNO = dataRow["拣配单号"].ToString();
                        F_PAEZ_WLNO = dataRow["物流单号"].ToString();
                        FYF = dataRow["运费"].ToString();

                        if (F_PAEZ_FBiILLNO == "" || F_PAEZ_WLNO == "" || FYF=="")
                        {
                            this.Model.SetValue("F_PAEZ_NOTE", "拣配单号,物流单号,金额不能为空,请检查", rowIndex);
                        }
                        else
                        {
                            strsql = "/*dialect*/ select count(*) from T_ECC_PICKDELIVERY where FBILLNO ='" + F_PAEZ_FBiILLNO + "' and FLOGISTICNUM ='" + F_PAEZ_WLNO + "'";
                            FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                            if (FCount=="0")
                            {
                                this.Model.SetValue("F_PAEZ_NOTE", "拣配单号与物流单号不正确,请检查", rowIndex);

                            }

                            else
                            {
                                strsql = "/*dialect*/ update T_ECC_PICKDELIVERY set F_PAEZ_YF = " + FYF + " where FBILLNO ='" + F_PAEZ_FBiILLNO + "' and FLOGISTICNUM ='" + F_PAEZ_WLNO + "'";
                                DBUtils.Execute(this.Context, strsql);
                                this.Model.SetValue("F_PAEZ_NOTE", "更新完毕", rowIndex);
                            }

                        }


                        // TODO : 设置其他字段值，此处略
                        rowIndex++;
                    }
                }
            }


            else if (e.Key.EqualsIgnoreCase("F_JD_BTNCancel"))
            {// 取消
                this.View.Close();
            }
        }

        /// <summary>
        /// 产生完整的文件名，包含了物理路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetFullFileName(string fileName)
        {
            string dir = "FileUpLoadServices\\UploadFiles";
            return PathUtils.GetPhysicalPath(dir, fileName);
        }


    }
}
